package kaoshi;

public class Quest3 {

    /*public static int minSum(int begin1,int end1) {
        int sum = 0,begin=0;
        int sum1=0;
        for (int i = 0; i < nums.length; i++) {
            if (sum<0){
                sum+=nums[i];
            } else {
                sum=nums[i];
                begin=i;
            }
            if (sum1>sum){
                sum1=sum;
                begin1=begin;
                end1=i;
            }
        }
        for (int i = begin1; i < end1; i++) {
            System.out.print(nums[i]+" ");
        }
        System.out.println();
        return sum1;
    }*/
    public static int maxSum(int []nums) {
        int sum = 0 ,sum1=-100,begin=0,begin1=0,end=0;
        for (int i =0;i<nums.length;i++){
            if (sum >0){
                sum += nums[i];
            }else{
                sum=nums[i];
                begin=i;
            }
            if (sum1<sum){
                sum1=sum;
                begin1=begin;
                end=i;
            }
        }
        for (int i = begin1; i <= end; i++) {
            System.out.print(nums[i]+" ");
        }
        System.out.println();
        return sum1;
    }

    public static void main(String[] args) {
        //   System.out.println(minSum(0,nums.length));
        int nums[]={-2,-3,-5,-1};
        System.out.println(maxSum(nums));
    }
}
